package com.dnzx.socket;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.*;

public class SocketListenerPusher implements Runnable {
    protected static Logger logger = LoggerFactory.getLogger(SocketListenerPusher.class);

    private ServerSocket serverSocket;
   // private ExecutorService pool;
    private ThreadPoolExecutor executor;
    private MessageHandler messageHandler;

    public SocketListenerPusher() {

    }

    public SocketListenerPusher(int port, int poolsize) {
        try {
            serverSocket = new ServerSocket();
            serverSocket.setReuseAddress(true);
            serverSocket.bind(new InetSocketAddress(port));
           // pool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * poolsize);
            executor = new ThreadPoolExecutor(1, 3, 10, TimeUnit.SECONDS, new LinkedBlockingDeque<Runnable>());
            //下面两句循环执行run()方法, 相当于while(true){...}
            ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(1, new DaemonThreadFactory());
            scheduledThreadPoolExecutor.scheduleAtFixedRate(this, 1L, 1L, TimeUnit.MILLISECONDS);

        } catch (NumberFormatException e) {
            logger.error(e.getMessage(), e);
            e.printStackTrace();
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
            e.printStackTrace();
        }

    }

    public SocketListenerPusher(int port, int poolsize, MessageHandler handler) {
        try {
            messageHandler = handler;
            serverSocket = new ServerSocket();
            serverSocket.setReuseAddress(true);
            serverSocket.bind(new InetSocketAddress(port));
          //  pool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * poolsize);

            //下面两句循环执行run()方法, 相当于while(true){...}
            ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1, new DaemonThreadFactory());
            executor.scheduleAtFixedRate(this, 1L, 1L, TimeUnit.MILLISECONDS);

        } catch (NumberFormatException e) {
            logger.error(e.getMessage(), e);
            e.printStackTrace();
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
            e.printStackTrace();
        }

    }
    @Override
    public void run() {
        Socket socket = null;
        try {
            socket = serverSocket.accept();
          //  pool.execute(new ConnectionThread(socket, this));
          //  executor.execute(new ConnectionThread(socket, this));
        } catch (IOException e) {
            System.out.println("线程池被关闭!!!!!!!!!!!");
         //   pool.shutdown();
            logger.error(e.getMessage(), e);
            e.printStackTrace();
        }

    }

    public void setMessageHandler(MessageHandler handler) {
        messageHandler = handler;
    }

    public MessageHandler getMessageHandler() {
        return messageHandler;
    }

}
